<?php

class ClsAppconfig extends ClsNaanalController
{
    public function __construct($controlpanel)
    {
        parent::__construct();
    }
    public function create()
    {
        $this->action='create';
        $arrMessage=array();
        $arrVarTmp=array();
        $arrSolution=array();
        if(!ClsNaanalApplication::$isControlPanelExist)
        {
            $arrMessage[]="ClsConfig::\$CONTROL_PANEL variable not exist";
            $arrSolution["control_panel"][]=" Create a variable ClsConfig::\$CONTROL_PANEL
";
        }
        if(ClsNaanalApplication::$controlPanelItemNotExist)
        {
            foreach(ClsNaanalApplication::$controlPanelItemNotExist as $controlPanel=>$arrTmp)
            {
                foreach($arrTmp as $controlPanleItem=>$data)
                {
                    if($controlPanleItem=="database" && $data===false)
                    {
                        $arrMessage[]="ClsConfig::\$CONTROL_PANEL['$controlPanel']['database'] not exist";
                        $arrSolution["control_panel"][]=" Create ClsConfig::\$CONTROL_PANEL['$controlPanel']['database']='your database name for this control panel'; for controlpanel - {$controlPanel}
";
                    }
                    if($controlPanleItem=="table")
                    {
                        if($data===false)
                        {
                            $arrMessage[]="ClsConfig::\$CONTROL_PANEL['$controlPanel']['table'] not exist";
                            $arrSolution["control_panel"][]=" Create ClsConfig::\$CONTROL_PANEL['$controlPanel']['table']=array('login'=>'your login table');  for controlpanel - {$controlPanel}
";
                        }
                        else
                        {
                            foreach($data as $k=>$d)
                            {
                                $arrMessage[]="ClsConfig::\$CONTROL_PANEL['$controlPanel']['table'][$k] not exist";
                                $arrSolution["control_panel"][]=" Create ClsConfig::\$CONTROL_PANEL['$controlPanel']['table'][$k]='your data';  for controlpanel - {$controlPanel}
";
                            }
                        }
                    }
                    if($controlPanleItem=="page")
                    {
                        if($data===false)
                        {
                            $arrMessage[]="ClsConfig::\$CONTROL_PANEL['$controlPanel']['page'] not exist";
                            $arrSolution["control_panel"][]=" Create ClsConfig::\$CONTROL_PANEL['$controlPanel']['page']=array
(
    'home'=>array
    (
        'module'=>'home',
        'action'=>'create'
    ),
    'login'=>array
    (
        'module'=>'user',
        'action'=>'login'
    ),
    'logout'=>array
    (
        'module'=>'logout',
        'action'=>'create'
    )
);  for controlpanel - {$controlPanel}
";
                        }
                        else
                        {
                            foreach($data as $k=>$d)
                            {
                                $arrMessage[]="ClsConfig::\$CONTROL_PANEL['$controlPanel']['page'][$k] not exist";
                                $arrSolution["control_panel"][]=" Create ClsConfig::\$CONTROL_PANEL['$controlPanel']['page'][$k]='your data';  for controlpanel - {$controlPanel}
";
                            }
                        }
                    }
                }
                
            }
        }
        if(!ClsNaanalApplication::$isWorkPlaceDirectoryExist)
        {
            $arrMessage[]="The 'workplace' directory not exist. ";
            $arrSolution["directory"][]=" Create a directory in the name of 'workplace' under application's root
";
        }
        if(!ClsNaanalApplication::$isClientDirectoryExist)
        {
            $arrMessage[]="The 'client' directory not exist. ";
            $arrSolution["directory"][]=" Create a directory in the name of 'client' under application's root
";
        }
        if(!ClsNaanalApplication::$isClientDefaultDirectoryExist)
        {
            $arrMessage[]="The 'client/default' directory not exist. ";
            $arrSolution["directory"][]=" Create a directory in the name of 'default' under client's directory
";
        }
        if(!ClsNaanalApplication::$isModulesDirectoryExist)
        {
            $arrMessage[]="modules directory under application root not exist";
            $arrSolution["directory"][]="create a 'modules' directory under application root";
        }
        if(!ClsNaanalApplication::$isLangDirectoryExist)
        {
            $arrMessage[]="lang directory under application root not exist";
            $arrSolution["directory"][]="create a 'lang' directory under application root";
            $arrSolution["directory"][]="create a 'default' directory under 'lang' directory";
        }
        if(!ClsNaanalApplication::$isLangDefaultDirectoryExist)
        {
            $arrMessage[]="default directory under lang directory not exist";
            $arrSolution["directory"][]="create a 'default' directory under 'lang' directory";
        }
        if(!ClsNaanalApplication::$isLangDefaultCommoneFileExist)
        {
            $arrMessage[]="lang/default/common.php not exist";
            $arrSolution["directory"][]="create a 'common.php' file under 'lang/default/' directory with following content.
&lt;?php
\$lang=array
(
    'HelloWorld'=>'Hello World!!!'
);
?&gt;";
        }
        if(!ClsNaanalApplication::$isLangWorkplaceDirectoryExist)
        {
            $arrMessage[]="lang directory under workplace not exist";
            $arrSolution["directory"][]="create a 'lang' directory under workplace directory";
            $arrSolution["directory"][]="create a 'default' directory under 'workplace/lang' directory";
        }
        if(!ClsNaanalApplication::$isLangWorkplaceDefaultDirectoryExist)
        {
            $arrMessage[]="default directory under workplace/lang directory not exist";
            $arrSolution["directory"][]="create a 'default' directory under 'workplace/lang' directory";
        }
        if(!ClsNaanalApplication::$isLangWorkplaceDefaultCommoneFileExist)
        {
            $arrMessage[]="workplace/lang/default/common.php not exist";
            $arrSolution["directory"][]="create a 'common.php' file under 'workplace/lang/default/' directory with following content.
&lt;?php
\$lang=array
(
    'HelloWorld'=>'Hello World!!!'
);
?&gt;";
        }
        if(!ClsNaanalApplication::$isLogDirectoryExist)
        {
            $arrMessage[]="log directory under application root not exist";
            $arrSolution["directory"][]="create a 'log' directory under application root";
        }
        if(ClsNaanalApplication::$arrDirectoryNotExist)
        {
            foreach(ClsNaanalApplication::$arrDirectoryNotExist as $directory=>$arrData)
            {
                $message=$arrData["message"];
                $solution=$arrData["solution"];
                $arrMessage[]=$message;
                $arrSolution["directory"][]=$solution;
            }
        }
        if(!ClsNaanalApplication::$isThemesDirectoryExist)
        {
            $arrMessage[]="themes directory under application root not exist";
            $arrSolution["directory"][]="create a 'themes' directory under application root";
        }
        if(!ClsNaanalApplication::$isHomeExist)
        {
            $arrMessage[]="file workplace/home.php|workplace/home_create.php (or) module 'home' not exist";
            $arrSolution["home"]="Either create a home.php|home_create.php at workplace directory under application root
or create a home module and add your home page content inside";
        }
        if(!ClsNaanalApplication::$isHookExist)
        {
            $arrMessage[]="file hooks.php not exist";
            $arrSolution["home"]="Create a hooks.php under application root";
        }
        if(!ClsNaanalApplication::$isClsConfigExist)
        {
            $arrMessage[]="class ClsConfig not exist in ClsConfig.php under application's root directory";
            $arrVarTmp[]="";
        }
        if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isDeveloperIPExist)
        {
            $arrMessage[]="Constant DEVELOPER_IP not defined";
            $arrVarTmp[]="";
        }
        //else 
        //{
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !isset(ClsConfig::$CONTROL_PANEL))
            {
                $arrMessage[]="The control panel static variable must set. ";
                $arrVarTmp[]="
    //If you not planned for login system, Add following static variable in ClsConfig class</br />
    public static \$CONTROL_PANEL=array();<br />

    //If you have plan for control panel, Add following static variable in ClsConfig class<br />
    public static \$CONTROL_PANEL=array
    (
        'user'=>array
        (
            'database'=>'your database name',
            'table'=>array
            (
                'login'=>'user'
            ),
            'page'=>array
            (
                'home'=>array
                (
                    'module'=>'home',
                    'action'=>'create'
                ),
                'login'=>array
                (
                    'module'=>'login',
                    'action'=>'create'
                ),
                'logout'=>array
                (
                    'module'=>'logout',
                    'action'=>'create'
                )
            )
        )
    );
    ";
            }
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isLogExist)
            {
                $arrMessage[]="Static variable ClsConfig::\$LOG not set";
                $arrVarTmp[]="  public static \$LOG=false (or) true;";
            }
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isLangVarExist)
            {
                $arrMessage[]="Static variable ClsConfig::\$LANG not set";
                $arrVarTmp[]="  public static \$LANG=false (or) language name (or) 'default';";
            }
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isDeveloperModeExist)
            {
                $arrMessage[]="Static variable ClsConfig::\$isDeveloperMode not set";
                $arrVarTmp[]="  public static \$isDeveloperMode=false (or) true;";
            }
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isDatabaseExist)
            {
                $arrMessage[]="Static variable ClsConfig::\$DATABASE not set";
                $arrVarTmp[]="public static \$DATABASE=array
(
    'your database name'=>array
    (
        'user'=>'your database user',
        'password'=>'your database password',
        'host'=>'your database host',
        'server'=>'mysql',
        'default'=> true (or) false //if default is true, this database will be the default database
    )
);";
            }
            if(!ClsNaanalApplication::$isDatabaseValid)
            {
                $arrMessage[]="database name invalid";
                $arrSolution["validity"]="Add valid database name";
            }
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isDisableTrackingExist)
            {
                $arrMessage[]="Static variable ClsConfig::\$DISABLE_TRACKING is not set";
                $arrVarTmp[]="  public static \$DISABLE_TRACKING=true;";
            }
            if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isThemeExist)
            {
                $arrMessage[]="Static variable ClsConfig::\$theme is not set";
                $arrVarTmp[]="  public static \$theme='default';";
            }
        //}
        
        if(!ClsNaanalApplication::$isErrINIExist)
        {
            $arrMessage[]="The err.ini not exist in application's root";
            $arrSolution["ini"]="Create err.ini file at the application's root
Usage:-<br />
Add following in err.ini</br />
LOGIN_ERROR=Unknown UserID or Password
";
        }
        
        $define="";
        if(!ClsNaanalApplication::$isClsConfigFileExist || !ClsNaanalApplication::$isClsConfigExist || !ClsNaanalApplication::$isDeveloperIPExist)
        {
            $define=$define."define('DEVELOPER_IP','127.0.0.1');";
        }
        
        if($arrVarTmp) 
        {
            $pref="";
            if(!ClsNaanalApplication::$isClsConfigFileExist)
            {
                $arrMessage[]="ClsConfig.php not exist in application's root directory";
                $pref=$pref."Create ClsConfig.php in application's root directory.\n";
            }
            if(!ClsNaanalApplication::$isClsConfigExist)
            {
                $pref=$pref."Create class ClsConfig in ClsConfig.php under application's root directory\n";
            }
            $arrSolution["config"]="
Usage : 

{$pref}
&lt;?php
{$define}
class ClsConfig
{
".implode("\n",$arrVarTmp)." 
}
?&gt;";
        }
        $message="";
        if($arrMessage) $message=implode("<br />",$arrMessage);
        $arrReturn=array('form'=>null,'issubmit'=>false,"message"=>$message);
        
        if(!isset($arrSolution["config"]) || empty($arrSolution["config"])) $arrSolution["config"]="Success";
        if(!isset($arrSolution["directory"]) || empty($arrSolution["directory"])) $arrSolution["directory"]="Success";
        if(!isset($arrSolution["ini"]) || empty($arrSolution["ini"])) $arrSolution["ini"]="Success";
        if(!isset($arrSolution["home"]) || empty($arrSolution["home"])) $arrSolution["home"]="Success";
        if(!isset($arrSolution["control_panel"]) || empty($arrSolution["control_panel"])) $arrSolution["control_panel"]="Success";
        if(!isset($arrSolution["validity"]) || empty($arrSolution["validity"])) $arrSolution["validity"]="Success";
        
        foreach($arrSolution as $var=>$data)
        {
            if(is_array($data))
            {
                $arrReturn[$var]=implode("\n",$data);
            }
            else
            {
                $arrReturn[$var]=$data;
            }
        }
        return $arrReturn;
    }

    function edit()
    {
        $obj=new ClsWAppconfig();
        $obj->loadRecord($_REQUEST['id']);
        return array('form'=>$obj);
    }

    function show_list()
    {
        $this->action='list';
        $arrWhere=array();
        $arrList = $this->showList($arrWhere,'ClsWAppconfig');
        return $arrList;
    }

    function update()
    {
        $obj=new ClsWAppconfig();
        $obj->setArray($_REQUEST);
        $obj->update();
        $pagevar=ClsConfig::$PAGINATION_VAR;
        $curpage=isset($_REQUEST[$pagevar])?$_REQUEST[$pagevar]:1;
        $this->redirect="{$this->urlModuleParam}=appconfig&{$this->urlActionParam}=list&{$pagevar}={$curpage}";
    }

    function insert()
    {
        $obj=new ClsWAppconfig();
        $obj->setArray($_REQUEST);
        $obj->update();
        $pagevar=ClsConfig::$PAGINATION_VAR;
        $curpage=isset($_REQUEST[$pagevar])?$_REQUEST[$pagevar]:1;
        $this->redirect="{$this->urlModuleParam}=appconfig&{$this->urlActionParam}=list&{$pagevar}={$curpage}";
    }
} 
?>
